/*
 * Javlov - a Java toolkit for reinforcement learning with multi-agent support.
 * 
 * Copyright (c) 2009 Matthijs Snel
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.javlov;

import java.util.Arrays;

/**
 * Wrapper class for continuous actions that allows any array to be passed in as values (i.e.
 * not necessarily of the dimensionality of the enclosed continuous action. The
 * array will be truncated or padded with zeros if its length is respectively larger or 
 * smaller than the dimensionality of the action this FlexContinuousAction is wrapping.
 * 
 * @author Matthijs Snel
 *
 */
public class FlexContinuousAction extends AbstractContinuousAction {

	private static final long serialVersionUID = -832200944218827565L;
	
	protected ContinuousAction action;
	
	public FlexContinuousAction(ContinuousAction act) {
		super();
		action = act;
		dim = act.getDimensionality();
	}

	@Override
	public double getMaxRange() {
		return action.getMaxRange();
	}

	@Override
	public double getMinRange() {
		return action.getMinRange();
	}

	@Override
	public double[] getValues() {
		return action.getValues();
	}

	@Override
	public void setValues(double[] vals) {
		double[] newvals = Arrays.copyOf(vals, dim);
		action.setValues(newvals);		
	}

	@Override
	public void execute(Agent a) {
		action.execute(a);		
	}
	
	@Override
	public boolean equals(Object obj) {
		if ( obj == null || !(obj instanceof FlexContinuousAction) )
			return false;
		FlexContinuousAction f = (FlexContinuousAction) obj;
		if ( dim != f.dim )
			return false;
		return action.equals(f.action);
	}
	
	@Override
	public int hashCode() {
		return 31*action.hashCode() + getClass().hashCode();
	}
}
